package com.kll.tv.views

import android.content.Intent
import android.view.KeyEvent
import android.view.View
import android.widget.TextView
import com.kll.carolinehmibiz.OperationManager
import com.kll.carolinehmibiz.model.AccountModel
import com.kll.carolinehmibiz.model.OperateListener
import com.kll.carolinehmibiz.net.bean.DailyResponse
import com.kll.carolinehmibiz.net.bean.RefreshTokenResponse
import com.kll.carolinehmibiz.presentation.listener.HealthIndexProtocol
import com.kll.carolinehmibiz.presentation.presenter.HealthIndexPresenter
import com.kll.tv.base.views.BaseActivity
import com.kll.tv.databinding.ActivityHealthIndexBinding
import com.kll.tv.utils.AppToast
import java.text.SimpleDateFormat
import java.util.*


/**
 * 每日健康指数页
 */
class HealthIndexActivity: BaseActivity<HealthIndexPresenter>(), HealthIndexProtocol.IView {
    private lateinit var binding: ActivityHealthIndexBinding
    private var valueList: ArrayList<TextView> = arrayListOf()
    private var subTitleList: ArrayList<TextView> = arrayListOf()
    private var titleList: ArrayList<TextView> = arrayListOf()
    private val cal = Calendar.getInstance()
    private var currentDay = ""

    override fun initViewBinding(): View {
        binding = ActivityHealthIndexBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun createPresenter(): HealthIndexPresenter {
        return HealthIndexPresenter(this, this)
    }

    override fun init() {
        valueList.add(binding.tvValue1)
        valueList.add(binding.tvValue2)
        valueList.add(binding.tvValue3)
        valueList.add(binding.tvValue4)
        valueList.add(binding.tvValue5)
        valueList.add(binding.tvValue6)
        valueList.add(binding.tvValue7)
        valueList.add(binding.tvValue8)
        valueList.add(binding.tvValue9)
        titleList.add(binding.tvTitle1)
        titleList.add(binding.tvTitle2)
        titleList.add(binding.tvTitle3)
        titleList.add(binding.tvTitle4)
        titleList.add(binding.tvTitle5)
        titleList.add(binding.tvTitle6)
        titleList.add(binding.tvTitle7)
        titleList.add(binding.tvTitle8)
        titleList.add(binding.tvTitle9)
        subTitleList.add(binding.tvSubTitle1)
        subTitleList.add(binding.tvSubTitle2)
        subTitleList.add(binding.tvSubTitle3)
        subTitleList.add(binding.tvSubTitle4)
        subTitleList.add(binding.tvSubTitle5)
        subTitleList.add(binding.tvSubTitle6)
        subTitleList.add(binding.tvSubTitle7)
        subTitleList.add(binding.tvSubTitle8)
        subTitleList.add(binding.tvSubTitle9)

        loadData()
        initListener()
    }

    private fun loadData() {
        // 获取当前日期
//        val format = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
//        val myDate = format.format(Date())
        cal.add(Calendar.DATE, 0)
        currentDay = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(cal.time)
        binding.tvDaily.text = currentDay
        mPresenter.getDaily(currentDay)
    }

    private fun initListener() {
        binding.ivBack.setOnClickListener { finish() }
        binding.ivBackCircular.setOnClickListener { startActivity(Intent(this, MainActivity::class.java))
            finish() }
        // 上一天
        binding.ivLeft.setOnClickListener {
            cal.add(Calendar.DATE, -1)
            currentDay = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(cal.time)
            binding.tvDaily.text = currentDay
            mPresenter.getDaily(currentDay)
        }
        // 下一天
        binding.ivRight.setOnClickListener {
            if (currentDay == SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(Date())) {
                AppToast.show("没有更多了", 2000)
            } else {
                cal.add(Calendar.DATE, +1)
                currentDay = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(cal.time)
                binding.tvDaily.text = currentDay
                mPresenter.getDaily(currentDay)
            }
        }

    }

    /**
     * 获取每日健康指数成功回调
     */
    override fun getDailySuccessed(data: ArrayList<DailyResponse>) {
        for ((index, i) in valueList.withIndex()) {
            i.text = data[index].value
        }
        for ((index, i) in subTitleList.withIndex()) {
            i.text = data[index].sub_title
        }
        for ((index, i) in titleList.withIndex()) {
            i.text = data[index].title
        }

    }

    /**
     * 获取每日健康指数失败回调
     */
    override fun getDailyFailed(code: Int, message: String) {
        AppToast.show(message, 2000)
        if (code == 102) {
            // token过期 刷新token
            OperationManager.getInstancce().refreshToken(object : OperateListener {
                override fun onSuccess(state: Int, obj: Any?) {
                    var data = obj as RefreshTokenResponse
                    AccountModel.getInstance(baseContext).updateUserToken(data.token)
                    mPresenter.getDaily(currentDay)
                }

                override fun onFail(code: Int, message: String) {
                    AppToast.show(message, 2000)
                    if (code == 101) {
                        loginAgain()
                    }
                }

            })
        }
        if (code == 101) {
            loginAgain()
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        when (keyCode) {
            KeyEvent.KEYCODE_DPAD_LEFT -> {
                cal.add(Calendar.DATE, -1)
                currentDay = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(cal.time)
                binding.tvDaily.text = currentDay
                mPresenter.getDaily(currentDay)
            }
            KeyEvent.KEYCODE_DPAD_RIGHT -> {
                if (currentDay == SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(Date())) {
                    AppToast.show("没有更多了", 2000)
                } else {
                    cal.add(Calendar.DATE, +1)
                    currentDay = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(cal.time)
                    binding.tvDaily.text = currentDay
                    mPresenter.getDaily(currentDay)
                }
            }
            else -> {
            }
        }
        return super.onKeyDown(keyCode, event)
    }


}